package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.dto.CourseTeacherDTO;
import com.xuecheng.api.content.model.vo.CourseTeacherVO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.content.common.constant.ContentErrorCode;
import com.xuecheng.content.convert.CourseTeacherConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.entity.CourseTeacher;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CourseTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.asm.Advice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author wyy
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    @Autowired
    private CourseBaseService courseBaseService;

    /**
     * 效验核心数据
     * 效验业务数据
     * 查询
     *
     * @param courseBaseId
     * @return
     */
    @Override
    public List<CourseTeacherDTO> queryCourseTeacherList(Long courseBaseId, Long companyId) {
        ExceptionCast.cast(ObjectUtils.isEmpty(courseBaseId), CommonErrorCode.E_100101);
//        CourseBase courseBase = courseBaseService.getById(courseBaseId);
        courseBaseService.getCourseBaseById(courseBaseId, companyId);
        LambdaQueryWrapper<CourseTeacher> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CourseTeacher::getCourseId, courseBaseId);
        List<CourseTeacher> list = this.list(queryWrapper);
        List<CourseTeacherDTO> courseTeacherDTOS = CourseTeacherConvert.INSTANCE.entitys2dtos(list);
        return courseTeacherDTOS;
    }

    /*
     *
     * @param courseTeacherDTO
     * @param vo
     * @return
     */
    @Override
    @Transactional
    public CourseTeacherDTO createOrModifyCourseTeacher(CourseTeacherDTO dto, Long companyId) {
        Long id = dto.getCourseTeacherId();
        Long courseId = dto.getCourseId();
//        String photograph = dto.getPhotograph();
        String position = dto.getPosition();
        String teacherName = dto.getTeacherName();
//        || StringUtil.isBlank(photograph)
        //效验数据
        if (ObjectUtils.isEmpty(courseId) || StringUtil.isBlank(position) || StringUtil.isBlank(teacherName)) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        //判断业务数据
        CourseBaseDTO courseBase = courseBaseService.getCourseBaseById(courseId, companyId);
        ExceptionCast.cast(!ObjectUtils.nullSafeEquals(courseBase.getCompanyId(), companyId), CommonErrorCode.E_100108);
        CourseTeacher courseTeacher = CourseTeacherConvert.INSTANCE.dto2entity(dto);
        //判断更新新增
        LambdaQueryWrapper<CourseTeacher> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CourseTeacher::getTeacherName,teacherName);
        queryWrapper.eq(CourseTeacher::getCompanyId,companyId);
        queryWrapper.eq(CourseTeacher::getCourseId,courseId);
        CourseTeacher one = this.getOne(queryWrapper);
        if (ObjectUtils.isEmpty(id)) { //新增
            if(one!=null){//判断教师名称在在同一机构 同一课程是否存在
                ExceptionCast.cast(ContentErrorCode.E_1200502);
            }
            courseTeacher.setCompanyId(companyId);
            boolean save = this.save(courseTeacher);
            ExceptionCast.cast(!save, ContentErrorCode.E_1200504);
        } else {
            if(one!=null){
               if(!ObjectUtils.nullSafeEquals(one.getId(),dto.getCourseTeacherId())){//判断修改的是否是同一条数据
                   ExceptionCast.cast(ContentErrorCode.E_1200502);
               }
            }
            boolean b = this.updateById(courseTeacher);
            ExceptionCast.cast(!b, ContentErrorCode.E_1200505);
        }
        CourseTeacher teacher = this.getById(courseTeacher.getId());
        return CourseTeacherConvert.INSTANCE.entity2dto(teacher);
    }

    /*
     * 删除教师信息
     * @param courseTeacherId
     * @param courseBaseId
     * @param companyId
     * @return
     */
    @Override
    @Transactional
    public boolean removeCourseTeacher(Long courseTeacherId, Long courseBaseId, Long companyId) {
        if (ObjectUtils.isEmpty(courseTeacherId) || ObjectUtils.isEmpty(courseBaseId) || ObjectUtils.isEmpty(courseBaseId)) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        //判断教师信息是否存在
        CourseTeacher courseTeacher = this.getById(courseTeacherId);
        ExceptionCast.cast(ObjectUtils.isEmpty(courseTeacher),ContentErrorCode.E_1200503);
        //效验课程信息是否在同一家课程
        ExceptionCast.cast(!ObjectUtils.nullSafeEquals(courseBaseId,courseTeacher.getCourseId()),ContentErrorCode.E_1200507);
        //是否在一个机构 ,且审核状态正确
        courseBaseService.getCourseBaseById(courseBaseId, companyId);

        boolean remove = this.removeById(courseTeacherId);
        ExceptionCast.cast(!remove,ContentErrorCode.E_1200506);
        return true;
    }
}
